<?php
/* Description: This file is used to edit product. */
#########IncludeFiles####################################################################################
include_once '../inc/config.php';
include_once("../inc/bootstrape.php");
require_once _SITE_PATH_.'classes/Vendor/Vendor.php';
require_once _SITE_PATH_.'classes/Category/Category.php';
require_once _SITE_PATH_.'classes/Master/Master.php';
require_once _SITE_PATH_.'classes/CommonUtility/CommonUtility.php';

$errMsg = $_GET['err'];
$action = trim($_REQUEST['action']);
$id = trim($_REQUEST['id']); 
if(empty($id)){
    header("Location: "._SITE_ADMIN_URL_."master/service_mng.php");
    exit;
}

if( $_POST['submit']=='Submit'){    
	$parms['id'] 				= $_POST['id'];
	$parms['name'] 				= $_POST['name'];
	$parms['category']			= $_POST['category'];
	$parms['subcategory'] 		= $_POST['subcategory'];
	$parms['subsubcategory']	= $_POST['subsubcategory'];
	$parms['description'] 		= $_POST['description'];
	if(!empty($_POST['attributes'])){
		$parms['attributes'] 		= implode(',',$_POST['attributes']);
	}
	$parms['tags'] 				= $_POST['tags'];
	$parms['added']				= date("Y-m-d h:i:s");
	    
    $condition = array(); 
    $condition['id'] = $parms['id'];
    if( CommonUtility::updateData($parms, 'services_master', $condition)) {        
        //  edit service attributes
//         $parms = array();
//         $parms['service_id'] = $id ;
//         CommonUtility::deleteData('services_attributes',$parms);        
    	//below lines are comment for removing attributes from master service            	
//         if(!empty($_POST['attributes'])){
//             foreach($_POST['attributes'] as $key => $val){
//             	$parms_condition = array();
//                 $parms_condition['service_id'] = $id;
//                 $parms_condition['attribute_id'] = $key;
//                 foreach($val as $inner_key => $inner_val){
//                     $parms_attributes['attribute_name'] = $inner_key;
//                     $parms_attributes['attribute_value'] = $inner_val;
//                 }
//                 CommonUtility::updateData($parms_attributes, 'services_attributes',$parms_condition);
//             }
//         }
        header("Location: service_mng.php?msg=1616");            
        exit;
    }
    $errMsg = 1617; 
}

$editor = CommonUtility::includeEditor();

$service_attributes = array();
$service_attributes = Category::getServiceAttributesNew($id);

if( $err ){
    $result = $_POST;
}else{
    $services = array();
	$services = Master::getServices($id);
	$result = $services[0];
}

$main_categories = array();
$main_categories = Category::getMainCategories();

$sub_categories = array();
$sub_categories = Category::getSubCategories($result['category']);

$sub_sub_categories = array();
$sub_sub_categories = Category::getSubSubCategories($result['subsubcategory'],1);
$subsubcat = $sub_sub_categories[0];

$arrServiceAttributes = array();
if($result['attributes'] != ''){
	$str_attributes = $result['attributes'];
	$arrServiceAttributes = explode(',',$str_attributes);
}

// $drop_down_attributes = '';
// $drop_down_attributes = CommonUtility::getServiceAttributes($arrServiceAttributes);

$errMsg = $err;
$_tplName = 'master/service_edit.tpl';
$_title = 'Edit Service';

require_once _ADMIN_TPL_MAIN_;
?>